package game.system;

import game.socket.GameThread;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Scanner;

public class CmdThread extends Thread{
	public static void main(String[] args) {
		CmdThread ct = new CmdThread();
		ct.start();
	}
	
	public void run(){
		Scanner scann = new Scanner(System.in);
		while(true){
			System.out.print("cmd:");
			String nl = scann.nextLine();
			try {
				String[] nls = nl.split(" ");
				Method method = CmdThread.class.getMethod(nls[0]);
				if(method!=null && method.isAnnotationPresent(CmdAnnotation.class) && method.getAnnotation(CmdAnnotation.class).isCmd()){
					method.invoke(this);
				}else{
					System.out.println("指令错误");
				}
			} catch (Exception e) {
				System.out.println("指令错误");
				e.printStackTrace();
			} 
		}
	}
	
	@CmdAnnotation(isCmd=true,cmdName="帮助")
	public void help(){
		Method[] methods = CmdThread.class.getDeclaredMethods();  
        for (Method method : methods){
            if (method.isAnnotationPresent(CmdAnnotation.class)) {   
            	CmdAnnotation annotation = method.getAnnotation(CmdAnnotation.class);   
                System.out.println("指令:" + method.getName() +"\t" + annotation.cmdName());   
            } 
        }   

	}
	
	@CmdAnnotation(isCmd=true,cmdName="在线线程数")
	public void threadCount(){
		Map<String,GameThread> map = GameThread.getGameThreads();
		System.out.println("当前线程数:"+GameThread.getGameThreads().size());
	}
	
	@CmdAnnotation(isCmd=true,cmdName="在线线程列表")
	public void threadList(){
		Map<String,GameThread> map = GameThread.getGameThreads();
		if(map.size()==0){
			System.out.println("当前在线线程为0");return;
		}
		for (String key:map.keySet()) {
			System.out.println("key:"+key+"\t"+map.get(key).getName());
		}
	}
	
	@CmdAnnotation(isCmd=true,cmdName="在线用户个数")
	public void userCount(){
		System.out.println("当前用户数:"+UserModel.getUserMap().size());
	}
	
	@CmdAnnotation(isCmd=true,cmdName="在线用户列表")
	public void userList(){
		Map<String,UserVo> map = UserModel.getUserMap();
		if(map.size()==0){
			System.out.println("当前在线用户为0");return;
		}
		for (String key:map.keySet()) {
			UserVo vo = map.get(key);
			System.out.println("id:"+vo.getId()+"\t"+vo.getName()+"\t"+vo.getIp());
		}
	}
}
